package android.slc.mp.ui.page;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.slc.ma.MaBaseAdapter;
import android.slc.mp.R;
import android.slc.mp.po.i.IBaseFolder;
import android.slc.mp.po.i.IBaseItem;
import android.slc.mp.po.i.IBaseResult;
import android.slc.mp.ui.SlcIMpView;
import android.slc.mp.ui.widget.SlcMpMaxHeightRecyclerView;
import android.slc.popup.SlcPopup2;
import android.slc.popup.fast.ShadowPopupWindow;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.color.MaterialColors;

import java.util.List;

/**
 * Created by slc
 * A placeholder fragment containing a simple view.
 */
public abstract class SlcMpPagerBaseVd<S extends IBaseResult<F, T>, F extends IBaseFolder<T>, T extends IBaseItem> implements SlcIMpView, SlcMpPagerVirtualVm.OnResultListener<List<T>>, View.OnClickListener {
    public final static String POSITION = "position";

    private FragmentActivity mActivity;

    private View mContentView;
    protected SlcMpPagerVirtualVm<S, F, T> mMpVirtualVm;

    //列表
    protected RecyclerView mediaPickerRecyclerView;
    protected SlcMpBaseMpAdapter<T> adapter;

    /**
     * 设置Vm
     */
    public void setMpVirtualVm(SlcMpPagerVirtualVm<S, F, T> mpVirtualVm) {
        this.mMpVirtualVm = mpVirtualVm;
    }

    protected FragmentActivity requireActivity() {
        return mActivity;
    }
    ///////////////  初始化 start  ////////////////

    public <V extends View> V findViewById(@IdRes int id) {
        return mContentView.findViewById(id);
    }

    @LayoutRes
    protected abstract int getContentView();

    public View onCreateView(FragmentActivity activity, @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mActivity = activity;
        mContentView = inflater.inflate(getContentView(), container, false);
        slcMtBindView(savedInstanceState);
        slcMtInitData();
        return mContentView;
    }

    @Override
    public void slcMtBindView(@Nullable Bundle savedInstanceState) {
        mediaPickerRecyclerView = findViewById(getMediaPickerViewId());
        mediaPickerRecyclerView.setLayoutManager(getMediaPickerRecyclerViewLayoutManager());
    }

    @Override
    public void slcMtInitData() {
        mMpVirtualVm.setOnDataRefreshListener(this);
        mMpVirtualVm.loader(requireActivity());
    }

    @IdRes
    protected abstract int getMediaPickerViewId();

    /**
     * 获取列表管理器
     *
     * @return
     */
    protected RecyclerView.LayoutManager getMediaPickerRecyclerViewLayoutManager() {
        return new GridLayoutManager(requireActivity(), mMpVirtualVm.getMediaPickerDelegate().getSpanCount());
    }

    protected abstract SlcMpBaseMpAdapter<T> getMediaPickerAdapter(List<T> data);

    /**
     * 刷新媒体列表
     *
     * @param data
     */
    protected void refreshMediaListView(List<T> data) {
        if (adapter == null) {
            adapter = getMediaPickerAdapter(data);
            adapter.setOnItemClickListener(new MaBaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(MaBaseAdapter<?> adapter, View view, int position) {
                    SlcMpPagerBaseVd.this.onItemClick(adapter, view, position);
                }
            });
            adapter.setOnItemChildClickListener(new MaBaseAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(MaBaseAdapter<?> adapter, View view, int position) {
                    SlcMpPagerBaseVd.this.onItemChildClick(adapter, view, position);
                }
            });
            mediaPickerRecyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    protected void refreshFolderInfo() {
        F allItemFolder = mMpVirtualVm.getResult().getAllItemFolder();
        //此处代码有问题
        setSelectFolderName(allItemFolder == null ? null : mMpVirtualVm.getResult().getAllItemFolder().getName());
    }

    /**
     * 点击事件
     *
     * @param adapter
     * @param view
     * @param position
     */
    protected void onItemClick(MaBaseAdapter<?> adapter, View view, int position) {
        //此处暂时注销
        //mMpVirtualVm.onItemClick(position);
    }

    /**
     * 子item点击事件
     *
     * @param adapter
     * @param view
     * @param position
     */
    protected void onItemChildClick(MaBaseAdapter<?> adapter, View view, int position) {
        if (view.getId() == getClickCheckId()) {
            boolean isSucceed = mMpVirtualVm.selectItem(position);
            //此处暂时不处理选择后的效果
            /*CheckBox checkBox;
            if (getClickCheckId() == getCheckId()) {
                checkBox = (CheckBox) view;
            } else {
                checkBox = (CheckBox) view.findViewById(getCheckId());
            }
            if (isSucceed) {
                checkBox.setChecked(!checkBox.isChecked());
            }*/
        } else {
            mMpVirtualVm.onItemChildClick(position);
        }
    }

    @IdRes
    protected int getClickCheckId() {
        return R.id.checkbox;
    }


    @IdRes
    protected int getCheckId() {
        return R.id.checkbox;
    }


    /**
     * 选择事件了这个item
     *
     * @param position
     * @param baseItem
     */
    public void onCheck(int position, IBaseItem baseItem) {
        SlcMpPagerBaseVd.this.adapter.notifyDataSwitch(position);
    }

    @Override
    public void onLoadResult(List<T> data) {
        refreshMediaListView(data);
        refreshFolderInfo();
    }

    ////////切换文件夹相关对话框和事件////////
    protected void showSwitchFolderDialog(View anchor) {
        SlcMpMaxHeightRecyclerView recyclerView = new SlcMpMaxHeightRecyclerView(requireActivity());
        int heightPixels = requireActivity().getResources().getDisplayMetrics().heightPixels;
        recyclerView.setMaxHeight((int) (heightPixels - heightPixels / 3f));
        recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
        MaBaseAdapter<?> slcBaseAdapter = getSwitchFolderAdapter();
        slcBaseAdapter.setOnItemClickListener(new MaBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MaBaseAdapter<?> adapter, View view, int position) {
                SlcPopup2.dismissByKey(String.valueOf(SlcMpPagerBaseVd.this.hashCode()));
                onSwitchFolderDialogItemClick(position);
            }
        });
        recyclerView.setAdapter(slcBaseAdapter);
        SlcPopup2.createShadowPopupOperate()
                .setKey(String.valueOf(hashCode()))
                .show(new ShadowPopupWindow(requireActivity())
                        .setContentView(recyclerView)
                        .setAnchor(anchor)
                        .setCancelable(true)
                        .setBgDrawable(new ColorDrawable(MaterialColors.getColor(anchor, R.attr.colorSurface))).create());
    }

    protected MaBaseAdapter<?> getSwitchFolderAdapter() {
        return new SlcMpFolderAdapter<>(requireActivity(), mMpVirtualVm.getResult().getFolders());
    }

    protected void onSwitchFolderDialogItemClick(int position) {
        F baseFolder = mMpVirtualVm.getResult().getFolders().get(position);
        setSelectFolderName(baseFolder.getName());
        mMpVirtualVm.setCurrentMediaList(baseFolder.getItems());
        adapter.notifyDataSetChanged();
    }

    /**
     * 设置文件夹名名称
     *
     * @param folderName
     */
    protected abstract void setSelectFolderName(String folderName);

    public void onDestroyView() {
        mMpVirtualVm.setOnDataRefreshListener(null);
        SlcPopup2.dismissByKey(String.valueOf(SlcMpPagerBaseVd.this.hashCode()));
    }
}